#!/bin/bash

version=5.17.11.3835

#install box64
"${DIRECTORY}/manage" install-if-not-installed 'Box64' || error "Box64 installation failed!"
if ! command -v box64 >/dev/null;then
  error "Box64 failed to install somehow!\n/usr/local/bin/box64 does not exist"
fi

# Get dependencies
install_packages libxcb-xtest0 libxcb-xfixes0 libturbojpeg0 '|' libturbojpeg pulseaudio-utils pulseaudio || exit 1

#refresh list of libraries
sudo ldconfig

#For testing, set to false if zoom folder is already there
if true;then
  echo "Downloading Zoom..."
  rm -rf "${HOME}/zoom" ~/zoom_x86_64.tar.xz* ~/zoom_x64_libs.zip* ~/zoom_x64_libs
  wget "https://zoom.us/client/${version}/zoom_x86_64.tar.xz" || wget 'https://zoom.com/client/latest/zoom_x86_64.tar.xz' || error 'Failed to download Zoom x86_64!'
  
  echo "Extracting..."
  tar -xf ~/zoom_x86_64.tar.xz || error 'Failed to extract Zoom x86_64!'
  rm -f ~/zoom_x86_64.tar.xz #who cares if this fails
fi

echo "Creating a Zoom button in the Main Menu..."
echo "[Desktop Entry]
Name=Zoom
Exec=box64 ${HOME}/zoom/ZoomLauncher %u
Icon=$(dirname "$0")/icon-64.png
Path=${HOME}/zoom/
Type=Application
Comment=x86_64 version of software platform used for teleconferencing using Box64
Categories=Network;
StartupNotify=true" > ~/.local/share/applications/zoom.desktop

#Associate with mimeapp
echo "Associating Zoom mimetypes..."
xdg-mime default zoom.desktop x-scheme-handler/zoomus x-scheme-handler/zoommtg

echo "Installation complete!"
